﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;

namespace UFSSWebApp
{
    public partial class myfiles : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        private ArrayList files = new ArrayList();
        UFSSservicereference.UFSSFile[] filearray;

        protected void Page_Load(object sender, EventArgs e)
        {
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            filearray = ussc.getFiles(uc);
            
            if (!IsPostBack)
                refresh();
        }

        private void refresh()
        {
            files.Clear();
            lstFiles.Items.Clear();
                foreach (UFSSservicereference.UFSSFile file in filearray)
                {
                    files.Add(file);
                    lstFiles.Items.Add(file.filename.ToString());
                }

        }

        protected void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (lstFiles.SelectedIndex != -1)
                {
                    Session.Remove("sub");
                    try
                    {
                        Session.Add("sub", filearray[lstFiles.SelectedIndex].subname.ToString());
                        lbtnSub.Enabled = true;
                    }
                    catch
                    {
                        Session.Add("sub", "Not in a subscription.");
                        lbtnSub.Enabled = false;
                    }
                    lblDLCount.Text = filearray[lstFiles.SelectedIndex].downloads.ToString();
                    lblFileSize.Text = Math.Ceiling((double)(filearray[lstFiles.SelectedIndex].filesize / 1024)).ToString() + "Kb";
                    lblUploaded.Text = filearray[lstFiles.SelectedIndex].dateadded.ToString();
                }
                else
                    lblError.Text = "An error has occured.";
            }
            catch
            {
                lblError.Text = "An error has occured.";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstFiles.SelectedIndex != -1)
            {
                try
                {
                    ussc.delFile(uc, lstFiles.SelectedItem.ToString());
                    refresh();
                }
                catch
                {
                    lblError.Text = "Error deleting file.";
                }
            }
            else
                lblError.Text = "Error deleting file.";
        }

        protected void lbtnSub_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/usersubs.aspx");
        }

        protected void btnDLFile_Click(object sender, EventArgs e)
        {
            if (lstFiles.SelectedIndex != -1)
                retrieveFile(uc, filearray[lstFiles.SelectedIndex].userid);
            else
                lblError.Text = "Please select a file.";
        }

        public bool retrieveFile(UFSSservicereference.UserCreds uc, int fileid)
        {
            UFSSservicereference.UFSSFile fileinfo;
            byte[] file = null;

            fileinfo = ussc.getFileInfo(uc, fileid);
            ussc.DownloadFile(uc, fileid, ref file);
            FileReceiver fr = new FileReceiver(fileinfo.filename, file);
            string path = fileinfo.hash + "\\" + fileinfo.filename;
            fr.WriteFile(path);
            Response.Buffer = true;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileinfo.filename);
            string mappath = Server.MapPath(path);
            Response.TransmitFile(Directory.GetCurrentDirectory() + "\\" + path);
            Response.End();
            return true;
        }
    }
}